package com.github.rexsheng.mybatis.core;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import com.github.rexsheng.mybatis.annotation.EnableMybatisExtension;
import com.github.rexsheng.mybatis.config.AutoInterceptorConfiguration;
import com.github.rexsheng.mybatis.config.BuilderConfiguration;
import com.github.rexsheng.mybatis.config.IDatabaseDialect;

/**
 * @author RexSheng
 * 2021年6月8日 下午9:14:13
 * @since 1.6.0
 */
public class AutoInterceptorScannerRegister implements ImportBeanDefinitionRegistrar, ResourceLoaderAware{

	private final static String DBTYPE_NAME_KEY = "db";
	
	private final static String BUILDER_NAME_KEY = "builder";
	
	private final static String MYBATIS_PLUS_NAME_KEY = "mybatisPlus";
		
    @SuppressWarnings("unused")
	private ResourceLoader resourceLoader;

    @SuppressWarnings("unchecked")
	@Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //1. 从BeanIocScan注解获取到我们要搜索的包路径
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableMybatisExtension.class.getName()));
        if (annoAttrs == null || annoAttrs.isEmpty()) {
            return;
        }
        Class<? extends IDatabaseDialect> dbType = (Class<? extends IDatabaseDialect>) annoAttrs.get(DBTYPE_NAME_KEY);
        Class<? extends BuilderConfiguration> builderConfiguration = (Class<? extends BuilderConfiguration>) annoAttrs.get(BUILDER_NAME_KEY);
        boolean mybatisPlus=(boolean)annoAttrs.get(MYBATIS_PLUS_NAME_KEY);
        BeanDefinition rootBeanDefinition = BeanDefinitionBuilder.rootBeanDefinition(AutoInterceptorConfiguration.class)
                .addConstructorArgValue(dbType).addConstructorArgValue(mybatisPlus).addConstructorArgValue(builderConfiguration).getBeanDefinition();
        registry.registerBeanDefinition("autoInterceptorConfiguration", rootBeanDefinition);
        
//      RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
//      rootBeanDefinition.setBeanClass(AutoConfiguration.class);
        
//        BeanDefinition rootBeanDefinition2 = BeanDefinitionBuilder.rootBeanDefinition(HttpServletRequestWrapperFilterConfig.class)
//                .addConstructorArgValue(includePath).getBeanDefinition();
//        registry.registerBeanDefinition("httpServletRequestWrapperFilterConfig", rootBeanDefinition2);
        // 2. 找到指定包路径下所有添加了BeanIoc注解的类，并且把这些类添加到IOC容器里面去
//        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
//        scanner.setResourceLoader(resourceLoader);
//        scanner.addIncludeFilter(new AssignableTypeFilter(EsLogInterceptorConfig.class));
////        scanner.addIncludeFilter(new AnnotationTypeFilter(EsLogInterceptor.class));
//        scanner.scan("com.sanlly.es.client.config");
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

}
